package com.bawei.park2211a.adapter

import android.graphics.Color
import android.util.Log
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.bawei.base.net.ApiService
import com.bawei.park2211a.R
import com.bawei.park2211a.mine.MineApplyEntity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.coroutines.launch
import javax.inject.Inject

class MineApplayAdapter @Inject constructor(var apiService: ApiService, var lifecycleOwner: LifecycleOwner) : BaseQuickAdapter<MineApplyEntity, BaseViewHolder>(R.layout.item_apply) {
    override fun convert(helper: BaseViewHolder, item: MineApplyEntity) {
        lifecycleOwner.lifecycleScope.launch {
            when (item.applytitle) {
                "车位申请" -> {
                    try {
                        val cart = apiService.cart(1)
                        item.applymsg = cart.data[0].areaName
                        item.applystaut = "待审批"
                        item.applytime = "2025-03-18"
                        helper.setText(R.id.apply_msg, item.applymsg)
                        helper.setText(R.id.apply_time, item.applytime)
                        helper.setText(R.id.apply_title, item.applytitle)
                        helper.setText(R.id.apply_staut, item.applystaut)
                        when (item.applystaut) {
                            "待审批" -> {
                                helper.setTextColor(R.id.apply_staut,Color.YELLOW)
                            }
                            "已通过" -> {
                                helper.setTextColor(R.id.apply_staut,Color.GREEN)
                            }
                            "已拒绝" -> {
                                helper.setTextColor(R.id.apply_staut,Color.RED)
                            }
                        }
                    } catch (e: Exception) {
                        Log.d("TAG", "convert: ${e.message}")
                    }
                }
                "来访申请" -> {
                    try {
                        val visit = apiService.visit()
                        item.applymsg = visit.data[0].appointmentTime
                        item.applystaut = "已通过"
                        item.applytime = visit.data[0].appointmentTime
                        helper.setText(R.id.apply_msg,"陈明远(销售部)"+ item.applymsg)
                        helper.setText(R.id.apply_time, item.applytime)
                        helper.setText(R.id.apply_title, item.applytitle)
                        helper.setText(R.id.apply_staut, item.applystaut)
                        when (item.applystaut) {
                            "待审批" -> {
                                helper.setTextColor(R.id.apply_staut,Color.YELLOW)
                            }
                            "已通过" -> {
                                helper.setTextColor(R.id.apply_staut,Color.GREEN)
                            }
                            "已拒绝" -> {
                                helper.setTextColor(R.id.apply_staut,Color.RED)
                            }
                        }
                    } catch (e: Exception) {
                        Log.d("TAG", "convert: ${e.message}")
                    }
                }
                "保修申请" -> {
                    try {
                        val order = apiService.order()
                        item.applymsg =
                            order.data[0].repairLocation + order.data[0].faultDescription
                        item.applystaut = "已拒绝"
                        item.applytime = order.data[0].repairTime
                        helper.setText(R.id.apply_msg, item.applymsg)
                        helper.setText(R.id.apply_time, item.applytime)
                        helper.setText(R.id.apply_title, item.applytitle)
                        helper.setText(R.id.apply_staut, item.applystaut)
                        when (item.applystaut) {
                            "待审批" -> {
                                helper.setTextColor(R.id.apply_staut,Color.YELLOW)
                            }
                            "已通过" -> {
                                helper.setTextColor(R.id.apply_staut,Color.GREEN)
                            }
                            "已拒绝" -> {
                                helper.setTextColor(R.id.apply_staut,Color.RED)
                            }
                        }
                    } catch (e: Exception) {
                        Log.d("TAG", "convert: ${e.message}")
                    }
                }
                "巡更申请" -> {
                    try {
                        val polling = apiService.polling()
                        item.applytime = polling.data[0].patrolTime
                        item.applystaut = "已通过"
                        item.applymsg = polling.data[0].taskName

                        helper.setText(R.id.apply_msg, item.applymsg)
                        helper.setText(R.id.apply_time, item.applytime)
                        helper.setText(R.id.apply_title, item.applytitle)
                        helper.setText(R.id.apply_staut, item.applystaut)
                        when (item.applystaut) {
                            "待审批" -> {
                                helper.setTextColor(R.id.apply_staut,Color.YELLOW)
                            }
                            "已通过" -> {
                                helper.setTextColor(R.id.apply_staut,Color.GREEN)
                            }
                            "已拒绝" -> {
                                helper.setTextColor(R.id.apply_staut,Color.RED)
                            }
                        }
                    } catch (e: Exception) {
                        Log.d("TAG", "convert: ${e.message}")
                    }
                }
            }
        }
    }
}